//
//  TimeLineChart.m
//  CultureTrade
//
//  Created by SZFT1 on 16/1/5.
//  Copyright (c) 2016年 cheng.zongxin. All rights reserved.
//

#import "TimeLineChart.h"
#import "TimeChartPoint.h"

@implementation TimeLineChart

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    [self initalization];
    return self;
}

- (void)initalization
{
    self.kLineWidth = 10.0f;
    _pointArray = [NSArray array];
}

- (void)setPointArray:(NSArray *)pointArray
{
    // 重写set方法，重绘视图
    _pointArray = pointArray;
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect {
    [self drawInteralLine];
    
    [self drawTimeLine];
    
    [self drawLastTimeLine];
}

- (void)drawInteralLine
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetShouldAntialias(context, NO);
    CGContextSaveGState(context); // 保存状态和恢复状态必须成对出现，否则出栈为空崩溃
    CGMutablePathRef path = CGPathCreateMutable();
    CGFloat interalValue = self.frame.size.height / 5;
    CGFloat length[2] = {3,2};
    for (int i = 0; i < 5; i++) {
        CGFloat y = interalValue * i;
        CGContextSetLineDash(context, 0, length, 2);
        CGPathMoveToPoint(path, NULL, 0, y);
        CGPathAddLineToPoint(path, NULL, self.frame.size.width, y);
    }
    
    CGContextAddPath(context, path);
    
    CGContextSetLineWidth(context, 0.5);
    CGContextSetShouldAntialias(context, YES);  // 是否抗锯齿
    CGContextSetRGBStrokeColor(context, 102/255.0f, 102/255.0f, 102/255.0f, 0.5);
    
    // 画最后一根实线
    CGContextDrawPath(context, kCGPathStroke);
    CGPathRelease(path);
    CGContextRestoreGState(context);
    
    CGMutablePathRef pathBottom = CGPathCreateMutable();
    CGPathMoveToPoint(pathBottom, NULL, 0, self.frame.size.height);
    CGPathAddLineToPoint(pathBottom, NULL, self.frame.size.width, self.frame.size.height);
    
    CGContextAddPath(context, pathBottom);
    
    CGContextSetLineWidth(context, 0.5);
    CGContextSetShouldAntialias(context, YES);  // 是否抗锯齿
    CGContextSetRGBStrokeColor(context, 102/255.0f, 102/255.0f, 102/255.0f, 0.5);
    
    
    CGContextDrawPath(context, kCGPathStroke);
    CGPathRelease(pathBottom);
}

- (void)drawTimeLine
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGMutablePathRef newPath = CGPathCreateMutable();
    CGMutablePathRef avePath = CGPathCreateMutable();
    TimeChartPoint *firstTimePoint = self.pointArray.firstObject;
    CGPathMoveToPoint(newPath, NULL, firstTimePoint.nowPriPoint.x, firstTimePoint.nowPriPoint.y);
    
    for (int i = 1; i < self.pointArray.count ; i++) {
        TimeChartPoint *timePoint = self.pointArray[i];
        CGPathAddLineToPoint(newPath, NULL, timePoint.nowPriPoint.x, timePoint.nowPriPoint.y);
    }
    CGContextSetLineWidth(context, 0.5);
    CGContextSetShouldAntialias(context, YES);  // 是否抗锯齿
    
    CGContextSetRGBStrokeColor(context, 255/255.0f, 149/255.0f, 0/255.0f, 1);
    CGContextAddPath(context, newPath);
    CGContextDrawPath(context, kCGPathStroke);
    CGPathRelease(newPath);
    
    CGContextSetRGBStrokeColor(context, 255/255.0f, 255/255.0f, 100/255.0f, 1);
    CGContextAddPath(context, avePath);
    CGContextDrawPath(context, kCGPathStroke);
    CGPathRelease(avePath);
}

- (void)drawLastTimeLine{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context); 
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGFloat length[2] = {3,2};
    CGContextSetLineDash(context, 0, length, 2);
    CGContextSetLineWidth(context, 1);
    CGContextSetShouldAntialias(context, YES);  // 是否抗锯齿
    
    
    
    // 初始化View会调用drawRect，未初始化直接取，引发崩溃
    TimeChartPoint *lastPoint = self.pointArray.lastObject;
    CGPathMoveToPoint(path, NULL, lastPoint.nowPriPoint.x, lastPoint.nowPriPoint.y);
    CGPathAddLineToPoint(path, NULL, self.frame.size.width, lastPoint.nowPriPoint.y);
    
    
    CGContextAddPath(context, path);
    
    CGContextSetRGBStrokeColor(context, 255/255.0f, 0/255.0f, 0/255.0f, 0.5);
    CGContextDrawPath(context, kCGPathStroke);
    CGPathRelease(path);
}

@end
